<!--
 * @Description: 设置员工角色，当前选中员工角色列表
 * @version: 电子名片2.0
 * @Author: 付小清
 * @Date: 2020-12-03 15:38:34
 * @LastEditors: 尹鑫
 * @LastEditTime: 2020-12-18 10:26:57
-->

<template>
  <div class="table-box">
    <!-- 表格 -->
    <el-table
      ref="refTable"
      v-loading="loading"
      :data="_tableData"
      style="width: 100%"
      :border="true"
      :size="size"
      :highlight-current-row="true"
      :header-cell-style="headerCellStyle"
      @row-click="rowClick"
      @selection-change="selectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" type="index" width="50" /> -->
      <el-table-column
        v-for="(item, index) in _roleJClumen"
        :key="index"
        :prop="item.prop"
        :label="item.label"
        align="center"
      />
      <template slot="empty">
        <div>{{ userId.length ? '暂无角色' : '请选择员工' }}</div>
      </template>
    </el-table>
  </div>
</template>

<script>
// 页面配置
import { roleJClumen } from '../count.js'
// 接口
import { permissionEmpRole } from '@/api/system'
export default {
  props: {
    // 用户ID数组
    userId: {
      type: Array,
      default: () => {
        return []
      }
    },
    // 选中
    selectionList: {
      type: Array,
      default: () => {
        return []
      }
    },
    // table数据
    tableData: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      loading: false,
      headerCellStyle: {
        background: '#eef1f6',
        color: '#606266',
        textAlign: 'center'
      }
    }
  },
  computed: {
    size() {
      return this.$store.getters.size
    },
    _roleJClumen: {
      get() {
        return roleJClumen
      },
      set(value) {
        this.$emit('update:roleJClumen', value)
      }
    },
    _selectionList: {
      get() {
        return this.selectionList
      },
      set(value) {
        this.$emit('update:selectionList', value)
      }
    },
    _tableData: {
      get() {
        return this.tableData
      },
      set(value) {
        this.$emit('update:tableData', value)
      }
    }
  },
  watch: {
    userId: {
      handler(val) {
        if (val.length) {
          this.getList()
        } else {
          this._tableData = []
        }
      }
    }
  },
  methods: {
    selectionChange(val) {
      this._selectionList = val
    },
    // 列表请求
    async getList() {
      try {
        const { data } = await permissionEmpRole({
          empIds: this.userId.toString()
        })
        // 翻页
        this._tableData = data
        this.loading = false
      } catch (e) {
        console.warn(e)
        // 出错返回信息
        this._tableData = []
        this.loading = false
      }
    },
    // 点击行选中
    rowClick(row) {
      let status = true
      this._selectionList.forEach((item, index) => {
        if (JSON.stringify(row) === JSON.stringify(item)) {
          status = false
          this._selectionList.splice(index, 1)
        }
      })
      if (status) {
        this.$refs.refTable.toggleRowSelection(row, true)
        this._selectionList.push(row)
      } else {
        this.$refs.refTable.toggleRowSelection(row, false)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-pagination {
  margin-top: 15px;
  text-align: right;
}
</style>
